package com.shopping.controller;

import com.shopping.pojo.Product;
import com.shopping.pojo.Result;
import com.shopping.security.JwtUtil;
import com.shopping.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private JwtUtil jwtUtil;


//    查询商品简略数据（名称、简介、价格）
    @GetMapping("/home/product")
    public Result PageProductList(){
        log.info("查询全部商品的信息");
        List<Product> productList=productService.list();
        return Result.success(productList);
    }
//    根据id查询产品
    @GetMapping("/home/{id}")
    public Result PageProductDetail(@PathVariable Integer id){
        log.info("根据id查询产品：{}",id);
        Product product=productService.getById(id);
        return Result.success(product);
    }
    //添加该用户该商品的浏览记录
    @PostMapping("/home/addlog")
    public Result addBrowseLog(@RequestHeader("Authorization") String token,@RequestBody Product product){
        // 去掉"Bearer "前缀
        token = token.replace("Bearer ", "");
        // 从JWT中提取顾客ID
        Integer customerId = jwtUtil.extractCustomerId(token);
        log.info("添加浏览记录，顾客id为：{}，商品为：{}", customerId,product);
        productService.addBrowseLog(product,customerId);
        return Result.success();
    }

//    返回大轮播图的资源地址
    @GetMapping("/home/images")
    public Result getLargeCarouselImage(){
        log.info("返回大的轮播图");
        List<String> imageUrls = new ArrayList<>();
        imageUrls.add("api/images/LargeImage/1.jpeg");
        imageUrls.add("api/images/LargeImage/2.jpeg");
        imageUrls.add("api/images/LargeImage/3.jpeg");
        imageUrls.add("api/images/LargeImage/4.jpeg");
        imageUrls.add("api/images/LargeImage/5.jpeg");
        imageUrls.add("api/images/LargeImage/6.jpeg");
        imageUrls.add("api/images/LargeImage/7.jpeg");
        imageUrls.add("api/images/LargeImage/8.jpeg");
        return Result.success(imageUrls);
    }

    //管理员添加商品
    @PostMapping("/adminhome/add")
    public Result add(@RequestBody Product product){
        log.info("管理员添加产品产品：{}",product);
        boolean add=productService.addProduct(product);
        if(add){
            return Result.success();
        }
        else {
            return Result.error("产品名称重复");
        }
    }
    //管理员删除商品
    @DeleteMapping("/adminhome/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("管理员删除商品:{}",id);
        productService.deleteProduct(id);
        return Result.success();
    }
    //管理员修改商品
    @PostMapping("/adminhome/update")
    public Result update(@RequestBody Product product){
        log.info("管理员修改商品:{}",product);
        boolean update=productService.updateProduct(product);
        if(update){
            return Result.success();
        }
        else {
            return Result.error("产品名称重复");
        }
    }
}
